import delay from '@/utils/delay';

interface Statetype {
  name: string;
  count: number;
}

export default {
  // 仓库的数据
  // ts可以用as给对象的属性加类型
  state: {
    name: 'zhangsan',
    count: 5,
  } as Statetype,

  // 这里写异步函数，相当于actions
  effects: {
    // *queryUser({ payload }: any, { call, put }: any) {
    // const { data } = yield call(queryUser, payload);
    // yield put({ type: 'queryUserSuccess', payload: data });
    // },

    *decrementAsync({ payload }: any, { call, put }: any) {
      yield delay(1000);
      yield put({ type: 'decrement', payload });
    },
  },

  // 这里写同步函数，用于改变state，相当于mutations
  reducers: {
    changeName(state: Statetype, { payload }: { payload: string }) {
      return {
        ...state,
        name: payload,
      };
    },
    increment(state: Statetype) {
      return {
        ...state,
        count: state.count + 1,
      };
    },
    decrement(state: Statetype, action: { payload: number }) {
      return {
        ...state,
        count: state.count - action.payload,
      };
    },
  },
};
